# coding:utf-8
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import QHBoxLayout, QVBoxLayout
from qfluentwidgets import MaskDialogBase, BodyLabel, IndeterminateProgressRing, setFont, FluentStyleSheet, isDarkTheme


class LoadingDialog(MaskDialogBase):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.hBox = QHBoxLayout(self.widget)
        self.vBox = QVBoxLayout()

        self.progressRing = IndeterminateProgressRing(self)
        self.titleLabel = BodyLabel('Loading...', self)
        self.contentLabel = BodyLabel('正在获取数据，请耐心等待...', self)

        self.__initWidgets()

    def __initWidgets(self):
        self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
        self.widget.setMinimumWidth(500)

        setFont(self.titleLabel, 25)
        self.contentLabel.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
        self.progressRing.setFixedSize(80, 80)

        self._hBoxLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.hBox.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft)
        self.vBox.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft)
        self.hBox.setSpacing(20)
        self.vBox.setSpacing(10)
        self.hBox.setContentsMargins(20, 20, 20, 20)

        self.vBox.addWidget(self.titleLabel)
        self.vBox.addWidget(self.contentLabel)

        self.hBox.addWidget(self.progressRing, 0, Qt.AlignmentFlag.AlignCenter)
        self.hBox.addLayout(self.vBox)

        c = 0 if isDarkTheme() else 255
        self.setMaskColor(QColor(c, c, c, 50))
        FluentStyleSheet.DIALOG.apply(self)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def title(self):
        return self.titleLabel.text()

    def setContent(self, content: str):
        if content:
            self.contentLabel.setVisible(True)
        else:
            self.contentLabel.setVisible(False)
        self.contentLabel.setText(content)

    def content(self):
        return self.contentLabel.text()


if __name__ == '__main__':
    import sys
    from PyQt6.QtWidgets import QApplication, QWidget

    app = QApplication(sys.argv)
    widget = QWidget()
    loadingDialog = LoadingDialog(widget)
    widget.show()
    sys.exit(app.exec())
